package itsm.isperp.framework.data.log;

import itsm.isperp.framework.security.domain.Resource;
import itsm.isperp.framework.utils.JsonBuilder;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;

import org.activiti.engine.impl.util.json.JSONObject;
import org.junit.Test;

/**
 * 
 * 
 * @author lizx
 * @date 2013-1-6
 * @version V1.0
 */

public class DatalogMethod {

	@Test
	public void WriteObject() throws UnsupportedEncodingException {

		Resource oldData = new Resource();

		oldData.setId("1");
		oldData.setResource("首页");

		ByteArrayOutputStream outOld = new ByteArrayOutputStream();
		ObjectOutputStream objectOutputStreamOld = null;
		ByteArrayOutputStream outNew = new ByteArrayOutputStream();
		ObjectOutputStream objectOutputStreamNew = null;

		try {
			objectOutputStreamOld = new ObjectOutputStream(outOld);
			objectOutputStreamNew = new ObjectOutputStream(outNew);
			objectOutputStreamOld.writeObject(oldData);

		} catch (IOException e) {
			throw new RuntimeException(e);
		}

		System.out.println(outOld.toByteArray());

	}

	@Test
	public void SerializObject() {

		Resource oldData = new Resource();

		oldData.setId("1"); 
		oldData.setResource("首页");


		Resource newData = new Resource();

		newData.setId("1"); 
		oldData.setResource("新首页");

		String str = JsonBuilder.buildNonDefaultBinder().toJson(oldData);
		String str2 = JsonBuilder.buildNonDefaultBinder().toJson(newData);

		System.out.println(str);

		JSONObject json = new JSONObject(str);

		System.out.println(json.getString("name"));

		//String[] names = { "id", "name" };
		JSONObject json2 = new JSONObject(str2);

		// /比较两个json字符串的值
		Iterator iter = json.keys();
		String result = "";
		while (iter.hasNext()) {
			String obj = iter.next().toString();

			String oldValue = json.getString(obj);

			String newValue = json2.getString(obj);

			if (!oldValue.equals(newValue))
				result += obj + "：" + oldValue + "->" + newValue + "；";

		}
		System.out.println(result);

	}
}
